package com.wangjiyuan.giftshuo.ui.fragment.listtop.other;

import com.wangjiyuan.giftshuo.bean.listtopbean.commentbean.CommentBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.everydaybean.EveryDayBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.maybelikebean.MayBeLikeBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.singleproductbean.SingleProductBean;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ZangSyeriong on 2016/11/15.
 * <p>
 * 通过Rxjava联系M和V
 */

public class ListTopPresenter implements ListTopContract.IListTopPresenter{

    private ListTopContract.IListTopModel iListTopModel = null;
    private ListTopContract.IListTopView iListTopView = null;

    public ListTopPresenter(ListTopContract.IListTopView iListTopView) {
        iListTopModel = new ListTopModel();
        this.iListTopView = iListTopView;
    }

    @Override
    public void getEveryDayData(int path, Map<String, String> params) {
        iListTopModel.getEveryDayData(path, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<EveryDayBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        iListTopView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iListTopView.showErrorInfo("网速不给力，获取数据失败");
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onNext(EveryDayBean everyDayBean) {
                        iListTopView.getEveryDayData(everyDayBean);
                    }
                });
    }

    @Override
    public void getMayBeLikeData(int path) {
        iListTopModel.getMayBeLikeData(path)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MayBeLikeBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        iListTopView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iListTopView.showErrorInfo("网速不给力，获取数据失败");
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onNext(MayBeLikeBean mayBeLikeBean) {
                        iListTopView.getMayBeLikeData(mayBeLikeBean);
                    }
                });
    }

    @Override
    public void getCommentData(int path) {
        iListTopModel.getCommentData(path)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CommentBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        iListTopView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iListTopView.showErrorInfo("网速不给力，获取数据失败");
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onNext(CommentBean commentBean) {
                        iListTopView.getCommentData(commentBean);

                    }
                });
    }

    @Override
    public void getSingleProductData(int path) {
        iListTopModel.getSingleProductData(path)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SingleProductBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        iListTopView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iListTopView.showErrorInfo("网速不给力，获取数据失败");
                        iListTopView.hideProgress();
                    }

                    @Override
                    public void onNext(SingleProductBean singleProductBean) {
                        iListTopView.getSingleProductData(singleProductBean);

                    }
                });
    }
}
